package com.sangeng.expression;

import com.sangeng.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义权限校验方法
 * @author bing_  @create 2022/1/5-21:42
 */
@Component("ex")
public class SGExpressionRoot {

    public boolean hasAuthority(String authority){
        // 获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        // 获取当前登录用户 id
        // Long userId = loginUser.getUser().getId();
        // 判断用户权限集合中是否存在authority
        return permissions.contains(authority);
    }
}